import plugin from '../../../lib/plugins/plugin.js'
import fs from 'node:fs'
import YAML from 'yaml'
import gsCfg from '../../genshin/model/gsCfg.js'
import puppeteer from '../../../lib/puppeteer/puppeteer.js'
import { pluginRoot, pluginResources } from '../model/path.js'
import Reply from '../model/reply.js'
import { createRequire } from 'module';

export class zmXiaogong extends plugin
{
	constructor ()
	{
   		super
		({
      			name: 'Xiaogong',
      			dsc: 'Xiaogong面板均衡器',
      			event: 'message',
      			priority: 9,
      			rule:
			[{
        				reg: '(#|\\\*)?(xiaogong)(0|1|2|3|4|5|6)(flzxz|amszg|rs|tkzy|zcddms|gz|pmzg)(1|2|3|4|5)(zy|yx)(atk|mastery)(bj|bs)(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45)',
        				fnc: 'zmXiaogong'
      			}]
    		})
  	}

	async zmXiaogong (e) 
	{
//获取指令
		//声明指令容纳变量
		let msg
		//排除异常指令
    		try { msg = e.msg.trim() } catch (e) { return false }
    		if ((typeof msg != 'string') || msg.constructor !== String) 
		{ 
			return false 
		}

//写入指令到in.json
		//获取最新require函数方式
		const require = createRequire(import.meta.url);
		//获取fs工具包
		const { writeFile } = require('fs');
		//声明地址
		const patha = './in.json';
		const pathb = './out.json';
		const pathc = './plugins/ZM/model/character/Xiaogong/Xiaogong.html';
		//writeFile写入指令到patha
		writeFile(patha, msg.toString() , (error) => {
  			if (error) 
			{
    				console.log('An error has occurred ', error);
    				return;
  			}
  			console.log('Data written successfully to disk');
		});

//cheild_process调用cmd指令行运行Xiaogong.exe
		//获取node:child_process工具包
		const { exec } = require('node:child_process');
		//exec调用指令行运行Xiaogong.exe
		exec('cmd.exe /c start ./plugins/ZM/model/character/Xiaogong/Xiaogong.exe');
		
		//输出花里胡哨占用时间，推后文本获取进程保证抓取到刷新后文本
		await this.reply('初始化角色命座信息······');
		await this.reply('初始化武器精炼信息······');
		await this.reply('初始化圣遗物信息······');
		await this.reply('计算中······');

//从out.json读取数据并录入到Xiaogong.html
		//二次获取fs工具包
		const fs = require('fs');
		//readFile写入生成代码至pathc
		fs.readFile(pathb, 'utf8', (error, data) => {
     			if(error){
        			console.log(error);
        			return;
     			}
			writeFile(pathc, data.toString() , (error) => {
  				if (error) 
				{
    					console.log('An error has occurred ', error);
    					return;
  				}
  				console.log('Data written successfully to disk');
			});     			
		})

//图像操作
		//获取puppeteer工具包
		const puppeteer = require('puppeteer');
		//背景图像异步操作
		/*
		(async () => {
  			const browser = await puppeteer.launch();
  			const page = await browser.newPage();
			//确定网页规格
  			await page.setViewport({
				width: 2520,
				height: 1200
			});
			//html生成图
			await page.goto('file:///E:/Redis-x64-5.0.14.1/Miao-Yunzai/plugins/ZM/model/character/Xiaogong/XiaogongBackGround.html'); 
			//Output the screenshot as an image
  			await page.screenshot({ path: './plugins/ZM/model/character/Xiaogong/XiaogongBackGround.png' }); 
 	 		await browser.close();
		})();
		*/
		//最终图像异步操作
		(async () => {
  			const browser = await puppeteer.launch();
  			const page = await browser.newPage();
			//确定网页规格
  			await page.setViewport({
				width: 2520,
				height: 1200
			});
			//html生成图
			await page.goto('file:///E:/Redis-x64-5.0.14.1/Miao-Yunzai/plugins/ZM/model/character/Xiaogong/Xiaogong.html'); 
			//Output the screenshot as an image
  			await page.screenshot({ path: './plugins/ZM/resource/img/Xiaogong.png' }); 
 	 		await browser.close();
			//发送图像
			await this.reply(segment.image('file://'+pluginResources+'/img/Xiaogong.png'));
		})();
        
		return false;
	}
}